package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.service.GoodsService;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    DtsGoodsProductService productService;
    @Autowired
    DtsGoodsSpecificationService specificationService;

    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsOrderGoodsMapper orderGoodsMapper;

    @Override
    public long goodsCount() {
        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("deleted",false);
        Integer count = dtsGoodsMapper.selectCount(wrapper);

        return count;
    }

    @Override
    public Map<String, Object> getPage(Integer page, Integer limit, String sort, String order, String number, String name, List<Integer> brandsId) {
        IPage iPage = new Page(page,limit);
        QueryWrapper wrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(number)){
            wrapper.eq("goods_sn",number);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.eq("name",name);
        }
        if(brandsId!=null && brandsId.size()>0){
            wrapper.in("brand_id",brandsId);
        }
        wrapper.eq("deleted",false);

        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        dtsGoodsMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());

        map.put("items",iPage.getRecords());
        return map;

    }

    @Override
    public void insert(GoodsAllinone goodsAllinone) {

        DtsGoods dtsGoods = goodsAllinone.getGoods();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("goods_sn",dtsGoods.getGoodsSn());
        Integer count1 = dtsGoodsMapper.selectCount(wrapper);
        if(count1>0){
            throw new RuntimeException("编号存在");
        }
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("name",dtsGoods.getName());
        Integer count2 = dtsGoodsMapper.selectCount(wrapper1);
      if(count2>0){
            throw  new RuntimeException("商品名称存在");
        }
        //添加商品
        dtsGoods.setAddTime(new Date());
        dtsGoods.setUpdateTime(new Date());
        dtsGoods.setDeleted(false);
        dtsGoodsMapper.insert(dtsGoods);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(dtsGoods.getId());
            productService.insert(product);
        }

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for(DtsGoodsAttribute attribute : attributes){
            attribute.setAddTime(new Date());
            attribute.setGoodsId(dtsGoods.getId());
            attributeService.insert(attribute);
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for(DtsGoodsSpecification dtsGoodsSpecification : specifications){

            dtsGoodsSpecification.setGoodsId(dtsGoods.getId());
            specificationService.insert(dtsGoodsSpecification);
        }
    }

    @Override
    public DtsGoods selectById(Integer goodsId) {
         QueryWrapper wrapper = new QueryWrapper();

        DtsGoods dtsGoods = dtsGoodsMapper.selectById(goodsId);
        System.out.println(dtsGoods);
        return dtsGoods;
    }

    @Override
    public void updateGoods(DtsGoods goods) {

        dtsGoodsMapper.updateById(goods);
    }

    @Override
    public List<Map<String, Object>> select() {
         List<Map<String,Object>> list = orderGoodsMapper.select();
         return list;
    }


}



